package com.example.ygp.index;

import com.example.ygp.entity.User;
import com.example.ygp.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author：ygp
 * @Package：com.example.ygp.index
 * @Project：db_demo2
 * @name：TestController
 * @Date：2023/12/24 14:02
 * @Filename：TestController
 */
@Slf4j
@RestController
public class TestController {


    @Autowired
    private UserMapper userMapper;

    @RequestMapping(value = "/adduser")
    public String adduser() {
        User user = new User();
        for (int i = 0; i < 10; i++) {
            user.setName("小明" + i);
//            user.setSex(1);
            user.setSex(2);
            if (userMapper.insert(user) == 1) {
                log.info("插入成功！");
            } else {
                log.info("插入失败！");
            }
        }
        return "add user ok";
    }

    @RequestMapping(value = "/adduser2")
    public String adduser2() {
        User user = new User();
        for (int i = 0; i < 10; i++) {
            user.setName("小明" + i);
            user.setSex(1);
//            user.setSex(2);
            if (userMapper.insert(user) == 1) {
                log.info("插入成功！");
            } else {
                log.info("插入失败！");
            }
        }
        return "add user2 ok";
    }

    @RequestMapping(value = "/add")
    public String add(@RequestParam(name = "sex") String sex) {
        User user = new User();
        for (int i = 0; i < 10; i++) {
            user.setName("小明" + i);
            user.setSex(Integer.parseInt(sex));
            if (userMapper.insertUser(user) == 1) {
                log.info("插入成功！");
            } else {
                log.info("插入失败！");
            }
        }
        return "add user2 ok";
    }

    @RequestMapping(value = "/query")
    public List<User> query(@RequestParam(name = "id") String id,@RequestParam(name = "sex") String sex) {
        User user = new User();
        user.setId(Long.parseLong(id));
        user.setSex(Integer.parseInt(sex));
        List<User> userList = userMapper.getUsers(user);
        log.info("查询结果：{}", userList);

        return userList;
    }


}
